package com.pinganfu.mock.test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;



import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
 
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
 
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

public class HttpClientService {
 
	public static  PoolingHttpClientConnectionManager cm	 = new  	PoolingHttpClientConnectionManager();
	public static  CloseableHttpClient httpclient;//=HttpClients.custom().setConnectionManager(cm).build();
	public static  HttpGet get=new HttpGet("http://www.apache.org/"); ;
	public static  HttpPost post = new HttpPost("http://www.apache.org/");

	//	cm = new PoolingHttpClientConnectionManager(); //澶氱嚎绋�澶歴ocket
	//	cm = new  	BasicHttpClientConnectionManager(); //澶氱嚎绋�鍗晄ocket,绾跨▼瀹夊叏
	static{
//		PoolingHttpClientConnectionManager cm ;
//		CloseableHttpClient httpClient;
//		Logger logger = LoggerFactory.getLogger(PostClient.class);
		cm = new PoolingHttpClientConnectionManager();
		cm.setDefaultMaxPerRoute(100);
	//	cm.setD
	//	cm.s
		cm.setMaxTotal(100);

		httpclient= HttpClients.custom().setConnectionManager(cm)
				.setMaxConnTotal(100).build();
	//	HttpClients.custom().setConnectionManager(cm).setKeepAliveStrategy(keepAliveStrategy)
	}
	public static void clearCookie(){
		cm = new PoolingHttpClientConnectionManager();
		cm.setDefaultMaxPerRoute(100);
		//	cm.setD
		//	cm.s
		cm.setMaxTotal(100);

		//httpclient.
		try {
			httpclient.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		httpclient=HttpClients.custom()
				.setConnectionManager(cm)
				.setMaxConnTotal(100)
				.setDefaultCookieStore(null)
				.build();

		  get=new HttpGet("http://www.apache.org/"); ;
	      post = new HttpPost("http://www.apache.org/");
	//	((BasicHttpClientConnectionManager)cm).getConnectionConfig().
		//cm.
	}

	public static CloseableHttpResponse sendPostRequest(String url, Map<String,String> params, Map<String, String> heads) throws ClientProtocolException, IOException, URISyntaxException {

		  post.setURI(new URI(url));
		  post.addHeader("Connection", "Keep-Alive");
          RequestConfig requestConfig = RequestConfig.custom()
                  .setSocketTimeout(10000)
                  .setConnectTimeout(10000)
                  .setConnectionRequestTimeout(10000)
                  .build();
          post.setConfig(requestConfig);
          
	    if(heads != null && heads.size()>0) {
			 for (String key : heads.keySet()) {
					post.addHeader(key, heads.get(key));
				}
		 }
		 
	    if (params != null && params.size()>0) {
	    	List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			Set<String> keyset = params.keySet();
			for (String key : keyset) {
				nvps.add(new BasicNameValuePair(key, params.get(key)));
			}
			post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
			  }

		CloseableHttpResponse response = httpclient.execute(post);
	//	httpclient.getConnectionManager().shutdown();
		return response;
	}
	
	public static CloseableHttpResponse sendGetRequest(String url) throws ClientProtocolException, IOException, URISyntaxException {
        
	//	Content-Type:text/html;charset=UTF-8
		 // get = new HttpGet(url);
			get.setURI(new URI(url));
          RequestConfig requestConfig = RequestConfig.custom()
                  .setSocketTimeout(10000)
                  .setConnectTimeout(10000)
                  .setConnectionRequestTimeout(10000)
                  .build();
          get.setConfig(requestConfig);
		CloseableHttpResponse response = httpclient.execute(get);
		get.addHeader("Content-Type","text/html;charset=UTF-8");
		
	//	httpclient.getConnectionManager().shutdown();
		
		return response;
		
	}
	/*
	 *  甯ookie Get
	 */
	public static CloseableHttpResponse sendGetRequest(String url,Map<String, String> headers) throws ClientProtocolException, IOException, URISyntaxException {


		get.setURI(new URI(url));
          RequestConfig requestConfig = RequestConfig.custom()
                  .setSocketTimeout(10000)
                  .setConnectTimeout(10000)
                  .setConnectionRequestTimeout(10000)
                  .build();
          get.setConfig(requestConfig);
		  get.addHeader("Connection", "Keep-Alive");
		 if(headers != null && headers.size()>0) {
			 for (String key : headers.keySet()) {
				 get.addHeader(key, headers.get(key));
				}
		 }
		 get.addHeader("Content-Type","text/html;charset=UTF-8");
			CloseableHttpResponse response = httpclient.execute(get);
		return response;
		
	}
	public static CloseableHttpResponse sendGetRequest(String url,Map<String, String> par,Map<String, String> headers) throws  Exception {

		get.setURI(new URI(url));
          RequestConfig requestConfig = RequestConfig.custom()
                  .setSocketTimeout(10000)
                  .setConnectTimeout(10000)
                  .setConnectionRequestTimeout(10000)
                  .build();
          get.setConfig(requestConfig);

		 if(headers != null && headers.size()>0) {
			 for (String key : headers.keySet()) {
				 get.addHeader(key, headers.get(key));
				}
		 }
		 //EntityUtils.
	  //   get.
		 List<NameValuePair> params = new ArrayList<NameValuePair>();
		 if(par != null && par.size()>0) {
			 for (String key : par.keySet()) {
				 params.add(  new BasicNameValuePair(key, par.get(key) ) );
				}
		 }
		//get.setU
		 String str = EntityUtils.toString(new UrlEncodedFormEntity(params));  
         get.setURI(new URI(get.getURI().toString() + "?" + str));  
		 //get.addHeader("Content-Type","text/html;charset=UTF-8");
		 //get.addHeader("Connection", "Keep-Alive");
		CloseableHttpResponse response = httpclient.execute(get);
		return response;
		
	}
	
	
	/*
		鍙戦�瀛楃涓�鎺ユ敹瀛楃涓�
	 */
	public static String post(String url,String ss)throws Exception
	{


		HttpPost  pt = new HttpPost(url);
		pt.setEntity(new StringEntity(ss,"utf-8"));

		HttpResponse resp = httpclient.execute(pt);
		String  pp=EntityUtils.toString(resp.getEntity(),"utf-8");

		return pp;
	}



}
